package com.example.brisk.rpc;

import com.example.brisk.entity.Book;
import com.example.brisk.vo.R;
import com.example.brisk.vo.req.BookSaveReqVo;
import com.example.brisk.vo.req.BookUpadateReqVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * book远程调用(rpc)服务
 * 使用service包装feign client增强数据及业务处理能力。
 */
@Service
public class BookRpcService {

    @Autowired
    private BookRpcClient bookRpcClient;

    public R<Book> getBookById(int id) {
        return bookRpcClient.getBookById(id);
    }

    public R saveBook(BookSaveReqVo vo) {
        return bookRpcClient.saveBook(vo);
    }

    public R updateBook(BookUpadateReqVo vo) {
        return bookRpcClient.updateBook(vo);
    }

    public R removeBook(int id) {
        return bookRpcClient.removeBook(id);
    }

    public  R<List<Book>> getBookList() {
        return bookRpcClient.getBookList();
    }

    /**
     * feign client : 通过简单的两行代码实现各种模式的http调用。
     * 若集成注册中心可实现客户端负载均衡。
     * url：路径前缀
     * name：feign client id
     * fallback： feign client fall back函数，当调用发生远程调用错误时，调用该函数。
     */
    // http
    @FeignClient(url = "localhost:8888/api/book", name = "boook-rpc-client", fallback = BookRpcServiceFallBack.class)
    // https
//    @FeignClient(url = "localhost:8888/api/book", name = "boook-rpc-client", configuration = FeignHttpsConfig.class, fallback = BookRpcServiceFallBack.class)
    public interface BookRpcClient {
        @GetMapping("/{id}")
        R<Book> getBookById(@PathVariable int id);

        @PostMapping("")
        R saveBook(@RequestBody BookSaveReqVo vo);

        @PutMapping("")
        R updateBook(@RequestBody BookUpadateReqVo vo);

        @DeleteMapping("/{id}")
        R removeBook(@PathVariable Integer id);

        @GetMapping("/list")
        R<List<Book>> getBookList();
    }
}